package com.jwater.core.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.jwater.core.utils.LoggerUtils;
import com.jwater.launch.HomePath;

/**
 * @description:
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
public class InitLogger {
    /**
     * 初始化bootstrap启动类日志
     */
    public static void initBootstrap() {
        String logPath = HomePath.getHomePath() + "/logs/bootstrap/bootstrap.log";
        String errPath = HomePath.getHomePath() + "/logs/bootstrap/bootstrap.err";
        System.setProperty("JWATER_LOG_FILE", logPath);
        System.setProperty("JWATER_ERROR_LOG_FILE", errPath);
        loadConfigFile();
    }

    /**
     * 初始化控制台日志打印
     */
    public static void initConsole() {
        String logPath = HomePath.getHomePath() + "/logs/console/console.log";
        String errPath = HomePath.getHomePath() + "/logs/console/console.err";
        System.setProperty("JWATER_LOG_FILE", logPath);
        System.setProperty("JWATER_ERROR_LOG_FILE", errPath);
        loadConfigFile();
    }

    /**
     * 初始化管理进程日志打印
     *
     * @param id
     */
    public static void initMaster(int id) {
        String logPath = HomePath.getHomePath() + "/logs/master/master-" + id + ".log";
        String errPath = HomePath.getHomePath() + "/logs/master/master-" + id + ".err";
        System.setProperty("JWATER_LOG_FILE", logPath);
        System.setProperty("JWATER_ERROR_LOG_FILE", errPath);
        loadConfigFile();
    }

    /**
     * 初始化代理进程日志的打印
     */
    public static void initProxy() {
        String logPath = HomePath.getHomePath() + "/logs/proxy/proxy.log";
        String errPath = HomePath.getHomePath() + "/logs/proxy/proxy.err";
        System.setProperty("JWATER_LOG_FILE", logPath);
        System.setProperty("JWATER_ERROR_LOG_FILE", errPath);
        loadConfigFile();
    }

    /**
     * 初始化工作进程日志打印
     *
     * @param regionName
     * @param deployName
     * @param groupName
     * @param workerName
     * @param index
     */
    public static void initWorker(String regionName, String deployName, String groupName, String workerName,
                                  int index) {
        String prefixPath = HomePath.getHomePath() + "/logs/" + regionName + "/" + deployName + "/" + groupName + "-"
                + workerName + "-" + index;
        String logPath = prefixPath + ".log";
        String errPath = prefixPath + ".err";
        System.setProperty("JWATER_LOG_FILE", logPath);
        System.setProperty("JWATER_ERROR_LOG_FILE", errPath);
        loadConfigFile();
    }

    /**
     * 初始化离线任务日志打印
     *
     * @param regionName
     * @param deployName
     * @param groupName
     * @param jobName
     */
    public static void initJob(String regionName, String deployName, String groupName, String jobName) {
        String prefixPath = HomePath.getHomePath() + "/logs/" + regionName + "/" + deployName + "/job/" + groupName
                + "-" + jobName;
        String logPath = prefixPath + ".log";
        String errPath = prefixPath + ".err";
        System.setProperty("JWATER_LOG_FILE", logPath);
        System.setProperty("JWATER_ERROR_LOG_FILE", errPath);
        loadConfigFile();
    }

    /**
     * 初始化logback日志打印
     */
    public static void loadConfigFile() {
        try {
            File file = new File(HomePath.getConfPath(), "logback.xml");
            if (file.exists()) {
                InputStream is = new FileInputStream(file);
                LoggerUtils.loadConfigFile(is);
                is.close();
            } else {
                LoggerUtils.loadConfigFile(null);
            }
        } catch (Exception e) {
            LoggerUtils.loadConfigFile(null);
        }
    }
}
